import { ElMessage, ElMessageBox } from "element-plus";
import { h } from "vue";

// 提示框
const toast = {
  success(msg: string) {
    ElMessage({
      message: h("p", { style: "line-height: 1; font-size: 14px" }, [
        h("span", null, "成功提示："),
        h("i", { style: "color: teal" }, msg),
      ]),
      type: "success",
    });
  },
  error(msg: string) {
    ElMessage({
      message: h("p", { style: "line-height: 1; font-size: 14px" }, [
        h("span", null, "错误提示："),
        h("i", { style: "color: teal" }, msg),
      ]),
      type: "error",
    });
  },
  info(msg: string) {
    ElMessage({
      message: h("p", { style: "line-height: 1; font-size: 14px" }, [
        h("span", null, "信息提示："),
        h("i", { style: "color: teal" }, msg),
      ]),
      type: "info",
    });
  },
  warning(msg: string) {
    ElMessage({
      message: h("p", { style: "line-height: 1; font-size: 14px" }, [
        h("span", null, "警告提示："),
        h("i", { style: "color: teal" }, msg),
      ]),
      type: "warning",
    });
  },
};
// 节流函数
function throttle(fn: (...args: any[]) => Promise<any>, delay: number = 1000) {
  let lastTime = 0;
  return function (...args: any[]) {
    const now = Date.now();
    if (now - lastTime > delay) {
      lastTime = now;
      return fn(...args); // 确保fn返回Promise
    }
    return Promise.resolve(); // 如果未执行fn，则返回一个resolved的Promise
  };
}
// 确认框
const confirmBox = (
  msg: string,
  confirm: Function = () => {},
  cancel: Function = () => {}
) => {
  ElMessageBox.confirm(msg, "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
    center: true,
  })
    .then(() => {
      confirm();
    })
    .catch(() => {
      cancel();
    });
};

export { toast, throttle, confirmBox };
